import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterzhuyili/main.dart';
import 'package:flutterzhuyili/utils/shared_preferences_keys.dart';
import 'package:flutterzhuyili/widgets/customWidgets.dart';

///@author xuziqiang
/// @date 2020/8/6 11:28
class ShuerteCustomPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _CustomPageState();
  }
}

class _CustomPageState extends State<ShuerteCustomPage> {
  bool isChildMode = false;
  bool isHardMode = false;
  bool isRandomMode = false;
  double rows = 3;
  double columns = 3;

  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '自定义游戏',
        ),
      ),
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              CheckboxListTile(
                  title: Text('简易模式'),
                  subtitle: Text('已点击的数据会变暗'),
                  value: isChildMode,
                  onChanged: (va) {
                    setState(() {
                      isChildMode = va;
                    });
                  }),
              CheckboxListTile(
                  title: Text('专业模式'),
                  subtitle: Text('点击错误，挑战失败'),
                  value: isHardMode,
                  onChanged: (va) {
                    setState(() {
                      isHardMode = va;
                    });
                  }),
              CheckboxListTile(
                  title: Text('随机模式'),
                  subtitle: Text('每次点击后，数字会重新随机分布'),
                  value: isRandomMode,
                  onChanged: (va) {
                    setState(() {
                      isRandomMode = va;
                    });
                  }),
              _buildRowsPickerWidget(),
              _buildColumnsPickerWidget(),
              _buildCreateBtn(),
            ],
          ),
        ),
      ),
    );
  }

  _buildRowsPickerWidget() {
    return Padding(
      padding: const EdgeInsets.only(left: 15, right: 15, top: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('列数：'),
          Slider(
              min: 1,
              max: 9,
              label: '${rows ~/ 1}',
              value: rows,
              divisions: 8,
              onChanged: (val) {
                setState(() {
                  rows = val;
                });
              }),
        ],
      ),
    );
  }

  _buildColumnsPickerWidget() {
    return Padding(
      padding: const EdgeInsets.only(left: 15, right: 15, top: 5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('行数：'),
          Slider(
              min: 1,
              max: 9,
              label: '${columns ~/ 1}',
              value: columns,
              divisions: 8,
              onChanged: (val) {
                setState(() {
                  columns = val;
                });
              }),
        ],
      ),
    );
  }

  _buildCreateBtn() {
    return Container(
        padding: EdgeInsets.fromLTRB(15, 15, 15, 0),
        child: SizedBox(
          width: double.infinity,
          height: 50,
          child: RaisedButton(
            color: Colors.green,
            shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(12.0))),
            child: Text(
              '开始游戏',
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () {
              sp.putInt(SharedPreferencesKeys.columns, columns.toInt());
              sp.putInt(SharedPreferencesKeys.rows, rows.toInt());
              sp.putBool(SharedPreferencesKeys.isChildMode, isChildMode);
              sp.putBool(SharedPreferencesKeys.isRandomMode, isRandomMode);
              sp.putInt(
                  SharedPreferencesKeys.canErrorTimes, isHardMode ? 0 : 3);
              List<String> _data = List();
              for (int i = 1; i <= rows * columns; i++) {
                _data.add('$i');
              }
              sp.putStringList(SharedPreferencesKeys.data, _data);

              Navigator.of(context).push(new MaterialPageRoute(
                builder: (parentContext) {
                  //指定跳转的页面
                  return MyGridView(
                    rows: rows.toInt(),
                    columns: columns.toInt(),
                    childModeTextStyle: TextStyle(
                        fontSize: _data.length > 36 ? 18 : 28,
                        fontWeight: FontWeight.bold,
                        color: Colors.black12),
                    singleBackgroundColor: Color(0x5526A69A),
                    backgroundColors: [Colors.blueGrey, Colors.blue],
                    singleTextStyle: TextStyle(
                        fontSize: _data.length > 36 ? 18 : 28,
                        fontWeight: FontWeight.bold,
                        color: Colors.white),
                    backgroundTextStyle: TextStyle(
                      color: Colors.black38,
                      fontSize: 88,
                      fontWeight: FontWeight.bold,
                    ),
                    data: _data,
                    isChildMode: isChildMode,
                    isRandomMode: isRandomMode,
                    canErrorTimes: isHardMode ? 0 : 3,
                  );
                },
              ));
            },
          ),
        ));
  }
}
